local quanmou = fk.CreateSkill {

  name = "joy__quanmou",

  tags = {  },

}



quanmou:addEffect("active", {
  name = "joy__quanmou",
  anim_type = "switch",
  switch_skill_name = "joy__quanmou",
  card_num = 0,
  target_num = 1,
  prompt = function ()
    return player:getSwitchSkillState("joy__quanmou", false) == fk.SwitchYang and "#joy__quanmou-Yang" or "#joy__quanmou-Yin"
  end,
  can_use = Util.TrueFunc,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    if #selected == 0 and not table.contains(player:getTableMark( "joy__quanmou_targets-phase"), to_select) then
      local target = Fk:currentRoom():getPlayerById(to_select)
      return not target:isNude() and player:inMyAttackRange(target)
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local mark = player:getTableMark( "joy__quanmou_targets-phase")
    table.insert(mark, target.id)
    room:setPlayerMark(player, "joy__quanmou_targets-phase", mark)
    local isYang = player:getSwitchSkillState(quanmou.name, true) == fk.SwitchYang
    local from_name = "joy_mou__simayi"
    local to_name = "joy_mou__simayi"
    if isYang then
      to_name = "joy_mou2__simayi"
    else
      from_name = "joy_mou2__simayi"
    end
    if player.general == from_name then
      player.general = to_name
      room:broadcastProperty(player, "general")
    end
    if player.deputyGeneral == from_name then
      player.deputyGeneral = to_name
      room:broadcastProperty(player, "deputyGeneral")
    end
    local card = room:askForCard(target, 1, 1, true, quanmou.name, false, ".", "#joy__quanmou-give::"..player.id)
    room:obtainCard(player, card[1], false, fk.ReasonGive, target.id)
    if player.dead or target.dead then return false end
    room:setPlayerMark(target, "@joy__quanmou-phase", isYang and "yang" or "yin")
    local mark_name = "joy__quanmou_" .. (isYang and "yang" or "yin") .. "-phase"
    mark = player:getTableMark( mark_name)
    table.insert(mark, target.id)
    room:setPlayerMark(player, mark_name, mark)
  end,
})

quanmou:addEffect(fk.GameStart, {
  name = "#joy__quanmou_switch",
  events = {fk.GameStart},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(quanmou.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    setTYMouSwitchSkillState(player, "simayi", "joy__quanmou",
    player.room:askForChoice(player, { "joy_mou_switch:::joy__quanmou:yang", "joy_mou_switch:::joy__quanmou:yin" },
    "joy__quanmou", "#joy_mou_switch-transer:::joy__quanmou") == "joy_mou_switch:::joy__quanmou:yin")
  end,
})

quanmou:addEffect(fk.DamageCaused, {
  name = "#joy__quanmou_delay",
  events = {fk.DamageCaused, fk.Damage},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local isYang = player:getSwitchSkillState(quanmou.name, false) == fk.SwitchYang
    if event == fk.GameStart and ((isYang and player.general == "joy_mou2__simayi") or (not isYang and player.general == "joy_mou__simayi")) then return true end
    if player.dead or player.phase ~= Player.Play or player ~= target then return false end
    if event == fk.DamageCaused then
      return table.contains(player:getTableMark( "joy__quanmou_yang-phase"), data.to.id)
    elseif event == fk.Damage then
      return table.contains(player:getTableMark( "joy__quanmou_yin-phase"), data.to.id)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local isYang = player:getSwitchSkillState(quanmou.name, false) == fk.SwitchYang
      local from_name = "joy_mou2__simayi"
      local to_name = "joy_mou2__simayi"
      if isYang then
        to_name = "joy_mou__simayi"
      else
        from_name = "joy_mou__simayi"
      end
      if player.general == from_name then
        player:broadcastSkillInvoke("joy__quanmou")
        room:notifySkillInvoked(player, "joy__quanmou")
        player.general = to_name
        room:broadcastProperty(player, "general")
      end
      if player.deputyGeneral == from_name then
        player:broadcastSkillInvoke("joy__quanmou")
        room:notifySkillInvoked(player, "joy__quanmou")
        player.deputyGeneral = to_name
        room:broadcastProperty(player, "deputyGeneral")
    end
    else
    room:doIndicate(player.id, {data.to.id})
    room:setPlayerMark(data.to, "@joy__quanmou-phase", 0)
    if event == fk.DamageCaused then
      local mark = player:getTableMark( "joy__quanmou_yang-phase")
      table.removeOne(mark, data.to.id)
      room:setPlayerMark(player, "joy__quanmou_yang-phase", mark)
      room:notifySkillInvoked(player, "joy__quanmou", "defensive")
      if player:getSwitchSkillState("joy__quanmou", false) == fk.SwitchYang then
        player:broadcastSkillInvoke("joy__quanmou")
      end
      return true
    elseif event == fk.Damage then
      local mark = player:getTableMark( "joy__quanmou_yin-phase")
      table.removeOne(mark, data.to.id)
      room:setPlayerMark(player, "joy__quanmou_yin-phase", mark)
      room:notifySkillInvoked(player, "joy__quanmou", "offensive")
      if player:getSwitchSkillState("joy__quanmou", false) == fk.SwitchYin then
        player:broadcastSkillInvoke("joy__quanmou")
      end
      local targets = table.filter(room.alive_players, function (p)
        return p ~= player and p ~= data.to
      end)
      if #targets == 0 then return false end
      targets = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 3, "#joy__quanmou-damage", "joy__quanmou")
      if #targets == 0 then return false end
      room:sortPlayersByAction(targets)
      for _, id in ipairs(targets) do
        local p = room:getPlayerById(id)
        if not p.dead then
          room:damage{
            from = player,
            to = p,
            damage = 1,
            skillName = "joy__quanmou",
          }
        end
      end
    end
    end
  end,
})
quanmou:addEffect(fk.Damage, {
  name = "#joy__quanmou_delay",
  events = {fk.DamageCaused, fk.Damage},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local isYang = player:getSwitchSkillState(quanmou.name, false) == fk.SwitchYang
    if event == fk.GameStart and ((isYang and player.general == "joy_mou2__simayi") or (not isYang and player.general == "joy_mou__simayi")) then return true end
    if player.dead or player.phase ~= Player.Play or player ~= target then return false end
    if event == fk.DamageCaused then
      return table.contains(player:getTableMark( "joy__quanmou_yang-phase"), data.to.id)
    elseif event == fk.Damage then
      return table.contains(player:getTableMark( "joy__quanmou_yin-phase"), data.to.id)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local isYang = player:getSwitchSkillState(quanmou.name, false) == fk.SwitchYang
      local from_name = "joy_mou2__simayi"
      local to_name = "joy_mou2__simayi"
      if isYang then
        to_name = "joy_mou__simayi"
      else
        from_name = "joy_mou__simayi"
      end
      if player.general == from_name then
        player:broadcastSkillInvoke("joy__quanmou")
        room:notifySkillInvoked(player, "joy__quanmou")
        player.general = to_name
        room:broadcastProperty(player, "general")
      end
      if player.deputyGeneral == from_name then
        player:broadcastSkillInvoke("joy__quanmou")
        room:notifySkillInvoked(player, "joy__quanmou")
        player.deputyGeneral = to_name
        room:broadcastProperty(player, "deputyGeneral")
    end
    else
    room:doIndicate(player.id, {data.to.id})
    room:setPlayerMark(data.to, "@joy__quanmou-phase", 0)
    if event == fk.DamageCaused then
      local mark = player:getTableMark( "joy__quanmou_yang-phase")
      table.removeOne(mark, data.to.id)
      room:setPlayerMark(player, "joy__quanmou_yang-phase", mark)
      room:notifySkillInvoked(player, "joy__quanmou", "defensive")
      if player:getSwitchSkillState("joy__quanmou", false) == fk.SwitchYang then
        player:broadcastSkillInvoke("joy__quanmou")
      end
      return true
    elseif event == fk.Damage then
      local mark = player:getTableMark( "joy__quanmou_yin-phase")
      table.removeOne(mark, data.to.id)
      room:setPlayerMark(player, "joy__quanmou_yin-phase", mark)
      room:notifySkillInvoked(player, "joy__quanmou", "offensive")
      if player:getSwitchSkillState("joy__quanmou", false) == fk.SwitchYin then
        player:broadcastSkillInvoke("joy__quanmou")
      end
      local targets = table.filter(room.alive_players, function (p)
        return p ~= player and p ~= data.to
      end)
      if #targets == 0 then return false end
      targets = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 3, "#joy__quanmou-damage", "joy__quanmou")
      if #targets == 0 then return false end
      room:sortPlayersByAction(targets)
      for _, id in ipairs(targets) do
        local p = room:getPlayerById(id)
        if not p.dead then
          room:damage{
            from = player,
            to = p,
            damage = 1,
            skillName = "joy__quanmou",
          }
        end
      end
    end
    end
  end,
})

return quanmou